package ar.uba.fi.tp2Unit.displayers;

import java.util.Observable;
import java.util.Observer;

import ar.uba.fi.tp2Unit.framework.TestResult;
import ar.uba.fi.tp2Unit.framework.TestResultDescription;
import ar.uba.fi.tp2Unit.framework.TestResultDisplayable;


/**
 * It displays the results of the execution of a test fixture
 * in the standard output.
 * 
 * @author grupo12
 * @see {@link TestResultDisplayable}
 */
public class ConsoleTestResultDisplayer implements TestResultDisplayable, Observer{

	private String lastTesSuiteName;
		
	public void display(TestResult testResult) {
		if (testResult.isFinished()){
			printNewLine(testResult.getSummary());			
		}
	}

	public void update(Observable testResult, Object testResultDescription) {
		if(!((TestResult) testResult).isFinished()){
			String name = ((TestResultDescription) testResultDescription).getTestSuiteName();
			if(!name.equals(lastTesSuiteName)){
				printTestSuiteName(name);
				lastTesSuiteName = name;
			}
			printNewLine(testResultDescription.toString());
		}		
	}
	
	private void printTestSuiteName(String testSuiteName) {
		printNewLine(String.format("\n%s", testSuiteName));
		String line ="";
		for (int i = 0; i < testSuiteName.length(); i++) {
			line+="-";					
		}
		printNewLine(line);
	}
	
	private void printNewLine(String line){
		System.out.println(line);
	}
}
